package com.example.zzl.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.example.zzl.R;
import com.example.zzl.utils.specification.VolleySingleton;

import java.util.List;

/**
 * Created by ligg on 2017/2/8.
 */

public class BaseCarouselPagerAdapter extends PagerAdapter {

    private ImageView[] viewsList;
    private Context context;
    private List<String> list;

    public BaseCarouselPagerAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
        if (list != null) {
            viewsList = new ImageView[list.size()];
            for (int i = 0; i < list.size(); i++) {
                ImageView imageView = new ImageView(context);
                viewsList[i] = imageView;
            }
        }
    }

    @Override
    public int getCount() {
        if (list != null && list.size() >0) {
            return Integer.MAX_VALUE;
        } else {
            return 0;
        }
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Log.e("CarouselPagerAdapter", "instantiateItem: " + position);

            position -= 200;
            position %= list.size();
            if (position < 0) {
                position += list.size();
            }

        Log.e("CarouselPagerAdapter", "instantiateItem: ======" + position);
       /* ImageView iv = viewsList[position];
        if (iv == null) {
            iv = new ImageView(context);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            viewsList[position] = iv;
        } else {
            ViewGroup parent = (ViewGroup) iv.getParent();
            parent.removeView(iv);
        }
*/
        final ImageView view = viewsList[position];
        //如果View已经在之前添加到了一个父组件，则必须先remove，否则会抛出IllegalStateException。
        ViewParent vp =view.getParent();
        if (vp!=null){
            ViewGroup parent = (ViewGroup)vp;
            parent.removeView(view);
        }
        String imgUrl = list.get(position);
        view.setTag(imgUrl);
        VolleySingleton.volleySingleton.getImageLoader().get(imgUrl, new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                if (view.getTag().equals(response.getRequestUrl())) {
                    Log.e("CarouselPagerAdapter", "onResponse: ");
                    view.setImageBitmap(response.getBitmap());
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("CarouselPagerAdapter", "onErrorResponse: " + error.getMessage());
            }
        });
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void notifyDataSetChanged() {
        if (list.size() != viewsList.length) {
            viewsList = new ImageView[list.size()];
            for (int i = 0; i < list.size(); i++) {
                ImageView imageView = (ImageView) View.inflate(context, R.layout.home_itemview, null);
                viewsList[i] = imageView;
            }
        }
        super.notifyDataSetChanged();
    }
}
